﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dropit.BlogPageProvider.Blog;

namespace Dropit.BlogPageProvider.WordPress
{
	public class Comment : IComment
	{
		public static List<Comment> List(JoeBlogs.WordPressWrapper wrapper, WordPressId id)
		{
			try
			{
				return wrapper.GetComments(id.PostId, "approve", 1000, 0).Select<JoeBlogs.Structs.Comment, Comment>(comment => new Comment()
				{
					WordPressId = new WordPressId()
					{
						BlogId = id.BlogId,
						PostId = id.PostId,
						CommentId = comment.comment_id
					},
					Heading = comment.post_title,
					Content = comment.content,
					Created = comment.date_created_gmt
				}).ToList();
			}
			catch (Exception ex)
			{
			}
			return new List<Comment>();
		}

		public static Comment Load(JoeBlogs.WordPressWrapper wrapper, WordPressId id)
		{
			return List(wrapper, id).First(comment => comment.WordPressId.CommentId == id.CommentId);
		}

		public static Comment SaveNew(JoeBlogs.WordPressWrapper wrapper, WordPressId parentId, IComment comment)
		{
			JoeBlogs.Structs.Comment wpComment = new JoeBlogs.Structs.Comment();
			wpComment.date_created_gmt = DateTime.UtcNow;
			wpComment.content = comment.Content;
			parentId.CommentId = wrapper.NewComment(parentId.PostId, wpComment);
			return Load(wrapper, parentId);
		}

		public WordPressId WordPressId
		{
			get;
			set;
		}

		#region IComment Members

		public string Id
		{
			get { return WordPressId.ToString(); }
		}

		public string Heading
		{
			get;
			set;
		}

		public string Content
		{
			get;
			set;
		}

		public DateTime Created
		{
			get;
			set;
		}

		#endregion

		#region IComment Members

		public IComment Create()
		{
			throw new NotImplementedException();
		}

		public void Save(IComment comment)
		{
			throw new NotImplementedException();
		}

		public void Delete(string id)
		{
			throw new NotImplementedException();
		}

		#endregion
	}
}
